﻿using System.Collections.ObjectModel;
using System.Windows;
using RAD.Modules.AD.Interfaces;
using RAD.Modules.AD.PresentationModels;

namespace RAD.Modules.AD.Views
{
    public class ADPresenter : IADPresenter
    {
        private ObservableCollection<ADItem> _dnCollection;
        private INetService _netService;


        public ADPresenter(IADView view, INetService netService, IADPresentationModel model)
        {
            View = view;
            _netService = netService;

            _dnCollection = netService.ADItemsCollection;

            model.Root = _dnCollection;
            model.Details = netService.DetailsItemsCollection;

            // Выводим имя текущей рабочей станции
            View.CurrentComputerName.Text = "Рабочая станция: " + netService.GetCurrentComputerName();

            // Выводим имя контроллера домена
            //View.CurrentComputerName.Text = "Контроллер домена: " + netService.GetDomainController();

            InitializeEvents();
        }

        private void InitializeEvents()
        {
            View.DomainTreeView.SelectedItemChanged +=
                new RoutedPropertyChangedEventHandler<object>(DomainTreeView_SelectedItemChanged);
        }

        private void DomainTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            ADItem fi = (ADItem) e.NewValue;
            fi.ChildForestItems.Clear();
            View.Model.Details.Clear();

            _netService.PopulateADPresentationModel(fi);
            //View.Model.Root = _dnCollection;
        }

        public IADView View
        {
            get;
            set;
        }

        public ObservableCollection<ADItem> DomainNamesList
        {
            get { return _dnCollection; }
        }
    }
}